"use strict";
const mongo = require("../../lib/mongo");
const USER = require("../object/user");
const path = require("path");
const ctrl = {};
ctrl.app = async (ctx) => {
    ctx.session.viewTime = new Date().getTime();
    let javascript = `window.appInfo.package = ${JSON.stringify(global.package)};\n`;
    if(ctx.session.userInfo){
        javascript+=`window.appInfo.user = ${JSON.stringify(ctx.session.userInfo)};\n`;

        let wechatUser = new USER(ctx.session.userInfo.openId);
        let userPackList = await wechatUser.getPackList();
        if(userPackList.length > 0){
            javascript+=`window.appInfo.userPackList = ${JSON.stringify(userPackList)};`;
        } else {
            javascript+=`window.appInfo.userPackList = [];`;
        }
    }
    await ctx.render('app', {base:'/ctrl/app', javascript, appName, settings:{autoescape: false}});
};
ctrl.demo = async (ctx) => {
    await ctx.render('index', {base:'/'});
};
ctrl.index = async (ctx) => {
    let loginStatus = false;
    if(ctx.session.userInfo){
        loginStatus = true;
    }
    let header = path.join(process.cwd(), "/front/frontend/tpl/page/account", "header.html");
    let footer = path.join(process.cwd(), "/front/frontend/tpl/page/account", "footer.html");
    await ctx.render('index', {loginStatus, footer, header});
};
ctrl.serviceTerm = async (ctx) => {
    let loginStatus = false;
    if(ctx.session.userInfo){
        loginStatus = true;
    }
    let header = path.join(process.cwd(), "/front/frontend/tpl/page/account", "header.html");
    let footer = path.join(process.cwd(), "/front/frontend/tpl/page/account", "footer.html");
    await ctx.render('html/service_term.html', {loginStatus, footer, header});
};
ctrl.registerAgreement = async (ctx) => {
    let loginStatus = false;
    if(ctx.session.userInfo){
        loginStatus = true;
    }
    let header = path.join(process.cwd(), "/front/frontend/tpl/page/account", "header.html");
    let footer = path.join(process.cwd(), "/front/frontend/tpl/page/account", "footer.html");
    await ctx.render('html/register_agreement.html', {loginStatus, footer, header});
};
module.exports = (_, pre = '/') =>{
    _.get(pre, ctrl.index);
    _.get(pre + "ctrl", ctrl.app);
    _.get(pre + "service_term", ctrl.serviceTerm);
    _.get(pre + "register_agreement", ctrl.registerAgreement);
    _.get(/^\/ctrl\/(?!api).*$/, ctrl.app);
    _.get(pre+ 'demo', ctrl.demo);
};